package com.makerworks.medu;

import android.content.Context;
import android.content.res.Configuration;
import android.support.v7.widget.AppCompatTextView;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;

/**
 * 7-14更新,支持wrap_content
 * <p/>
 * 这是一个single line自适应高度的TextView,仅通过高度设置字体,不考虑横向是否能显示完整(我会告诉你这就是需求?)
 * Created by liaoyuhao on 2016/6/14.
 */
public class FitHeightTextView extends AppCompatTextView {
    private TextPaint tmpTextPaint;
    private CharSequence text = null;

    public FitHeightTextView(Context context) {
        super(context);
        init(context, null, 0);
    }

    //lyh 横竖屏切换的bug..... 不清楚到底是百分比布局造成的还是别的原因造成的
    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            postDelayed(new Runnable() {
                @Override
                public void run() {
                    setText(getText());
                }
            }, 50);
        }

    }

    public FitHeightTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public FitHeightTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (h != oldh) {
            adjustTextSize();
        }
    }

    private void adjustTextSize() {
        tmpTextPaint = new TextPaint(getPaint());
        final int textSize = searchMaxTextSize(1, 10000);
        if (textSize == 1 || textSize == (int) getTextSize()) {
            return;
        }
        super.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        adjustTextSize();
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        setEllipsize(TextUtils.TruncateAt.END);             //显示不全就在最后省略
        setMaxLines(1);                                     //single line
//        setGravity(Gravity.CENTER_VERTICAL); //靠左居中显示
        setIncludeFontPadding(false);                       //上标下标去掉
    }

    private int searchMaxTextSize(int minSize, int maxSize) {
        int lastBest = minSize, start = minSize, end = maxSize, middle;
        while (start <= end) {
            middle = (start + end) >>> 1;
            if (canContain(middle)) {
                start = middle + 1;
                lastBest = middle;
            } else {
                end = middle - 1;
            }
        }
        return lastBest;
    }

    /**
     * 判断是否能显示得下
     *
     * @param size
     * @return
     */
    private boolean canContain(int size) {
        tmpTextPaint.setTextSize(size);
        StaticLayout layout = new StaticLayout(getText(), tmpTextPaint, 100000,/*随便输入的数字,这里不care宽度*/
                Layout.Alignment.ALIGN_NORMAL, 1, 0, false);
        int textHeight = layout.getHeight();
        int textAvailableHeight = getHeight();
        if (textAvailableHeight == 0) {
//            if (!getParent().isLayoutRequested())
            textAvailableHeight = getMeasuredHeight();
        }
        textAvailableHeight = textAvailableHeight - getCompoundPaddingBottom() - getCompoundPaddingTop();
        return textHeight <= textAvailableHeight;
    }

}